<template>
  <div class="body">
    <el-form ref="ruleForm" :model="mdl" label-width="130px">

        <el-form-item label="选择角色" required>
          <el-select v-model="mdl.roleIds" placeholder="请选择">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      
    </el-form>
    <div class="foot_btn">
      <el-button
        type="primary"
        :loading="submitLoading"
        @click="submitForm('ruleForm')"
      >提交</el-button>
      <el-button @click="handleCloseDrawer">取消</el-button>
    </div>
  </div>
</template>

<script>
import { addSysRoleUser,getSysRolePage } from '@/api/user'
export default {
  name: 'FormModel',
  props: {
    mdl: {
      type: Object,
      required: true
    },
    handleCloseDrawer: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      submitLoading: false,
      condition: {
        current: 1,
        size: 10,
      },
      roleList:[]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    
    // 获取角色列表
    getList() {
      getSysRolePage(this.condition).then(res => {
        if (res.statusCode === '00000') {
          this.roleList = res.data.records
        }
      })
    },

    // 提交
    submitForm() {
      this.mdl.roleIds = [this.mdl.roleIds]
      const request = addSysRoleUser(this.mdl)
      request.then((res) => {
        if (res.statusCode === '00000') {
          this.$message({ message: '提交成功', type: 'success' })
          // 关闭弹窗
          this.handleCloseDrawer()
          // 调用父级组件刷新数据
          this.$emit('getList')
        }
      })
        .catch((error) => {
          console.log(error)
        })
        .finally(() => {
        })
    }
  }
}
</script>

<style lang="less" scoped>
.body {
  padding-right: 25px;
  .foot_btn{
    width:150px;
    margin: 0 auto;
  }
}

/deep/ .el-checkbox__label {
  font-size: 12px;
}
</style>
